﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution113
    {
        private IList<IList<int>> res = new List<IList<int>>();

        public IList<IList<int>> PathSum(TreeNode root, int targetSum)
        {
            IList<int> path = new List<int>();
            DFS(root, path, 0, targetSum);
            return res;
        }

        private void DFS(TreeNode root, IList<int> path, int curSum, int targetSum)
        {
            path.Add(root.val);
            curSum += root.val;

            if (root.left == null && root.right == null)
            {
                //遇到叶子节点，判断，并收集
                if (curSum == targetSum)
                {
                    res.Add(new List<int>(path));
                }

                return;
            }
            //收集当前顶点

            if (root.left != null)
            {
                DFS(root.left, path, curSum, targetSum);
                path.RemoveAt(path.Count - 1);
            }
            if (root.right != null)
            {
                DFS(root.right, path, curSum, targetSum);
                path.RemoveAt(path.Count - 1);
            }
        }
    }
}